package controllers

import (
	"GO_BACK/api/dbapi"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

type info struct {
	Id       int64
	Password string
}

// postgersql
func LoginController(c *gin.Context) {
	var _info *info
	err := c.BindJSON(&_info)
	if err != nil {
		fmt.Println("err", err)
	}
	type Limitinfo struct {
		Id    int64  `json:"id"`
		Name  string `json:"name"`
		Email string `json:"email"`
	}

	result := dbapi.QuerybyidPwd(_info.Id, _info.Password)
	if result != nil && result.Id != 0 {
		resultinfo := Limitinfo{
			Id:    result.Id,
			Name:  result.Name,
			Email: result.Email,
		}
		c.JSON(http.StatusOK, gin.H{
			"message": "登录成功",
			"info":    resultinfo,
			"result":  result,
		})
	} else {
		fmt.Println("数据为空")
		fmt.Println(result)
		c.JSON(http.StatusBadRequest, gin.H{
			"message": "登录失败",
		})
	}
}
